package ssi.staaar.view;

import ssi.peeno.model.Message;
import ssi.staaar.ARActivity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Region;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class StaaarMessageView extends View {
	protected float x;
	protected float y;
	protected float scale = 1.0f;
	protected float x_length = 33.0f;
	protected float y_length = 25.0f;
	protected float screenWidth;
	protected float screenHeight;
	protected Message msg;
	protected boolean touched = false;
	protected Paint myPaint;
	protected Paint textPaint;
	protected int alpha = 100;
	protected int red = 0;
	protected int green = 0;
	protected int blue = 0;
	protected int textColor = Color.WHITE;
	protected float textSize = 5.0f;
	protected float left_value;
	protected float right_value;
	protected float top_value;
	protected float bottom_value;
	protected ARActivity activity;
	private long firstTouchTime;
	private RelativeLayout relativeL;
	private Context context;

	public StaaarMessageView(Context context, Message cur, ARActivity act) {
		super(context);
		msg = cur;
		this.activity = act;
		setMyPaint();
		setTextPaint();
	}
	
	public void setMyPaint() {
		myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		myPaint.setARGB(alpha, red, green, blue);
		myPaint.setAntiAlias(true);
	}
	
	public void setTextPaint() {
		textPaint = new Paint(Paint.LINEAR_TEXT_FLAG);
		textPaint.setColor(textColor);
		textPaint.setTextSize(textSize);
		textPaint.setAntiAlias(true);
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public float getScale() {
		return scale;
	}

	public void setScale(float scale) {
		this.scale = scale;
	}

	public float getX_length() {
		return x_length;
	}

	public float getY_length() {
		return y_length;
	}

	public float getScreenWidth() {
		return screenWidth;
	}

	public void setScreenWidth(float screenWidth) {
		this.screenWidth = screenWidth;
	}

	public float getScreenHeight() {
		return screenHeight;
	}

	public void setScreenHeight(float screenHeight) {
		this.screenHeight = screenHeight;
	}

	public Message getMessage() {
		return msg;
	}

	public void setMessage(Message msg) {
		this.msg = msg;
	}

	public RelativeLayout getRelativeL() {
		return relativeL;
	}

	public void setRelativeL(RelativeLayout relativeL) {
		this.relativeL = relativeL;
	}

	public boolean isPrivate() {
		return false;
	}

	public boolean isTouched() {
		return touched;
	}

	public void onDraw(Canvas c) {
		left_value = x - x_length;
		right_value = x + x_length;
		top_value = y - y_length;
		bottom_value = y + y_length;
		c.drawRoundRect(new RectF(left_value, top_value, right_value, 
				bottom_value), 4.0f, 3.0f, myPaint);
		if (touched) {
			//creare RelativeLayout interno
			RelativeLayout innerRelative = new RelativeLayout(context);
			int width = (int)(x_length*2*scale);
	        int height = (int)(y_length*2*scale);
			RelativeLayout.LayoutParams IRparams = new RelativeLayout.LayoutParams(width, height);
			int x = (int)(screenWidth - width)/2;
			int y = (int)(screenHeight - height)/2;
			IRparams.setMargins(x, y, 0, 0);
			innerRelative.setGravity(Gravity.CENTER_HORIZONTAL);
			innerRelative.setId(1000);
			innerRelative.setLayoutParams(IRparams);
			//creare TextView autore
			TextView author = new TextView(context);
			author.setTextSize(22.0f);
			author.setText(msg.getAuthor());
			author.setTextColor(Color.WHITE);
			author.setId(1001);
			author.setGravity(Gravity.CENTER_HORIZONTAL);
			RelativeLayout.LayoutParams authorLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
	        		RelativeLayout.LayoutParams.WRAP_CONTENT);
			innerRelative.addView(author, authorLP);
			//creare TextView subject
			TextView subject = new TextView(context);
			subject.setTextSize(22.0f);
			subject.setText(msg.getSubject());
			subject.setTextColor(Color.WHITE);
			subject.setId(1002);
			subject.setGravity(Gravity.CENTER_HORIZONTAL);
			RelativeLayout.LayoutParams subjectLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
	        		RelativeLayout.LayoutParams.WRAP_CONTENT);
			subjectLP.addRule(RelativeLayout.BELOW, author.getId());
			innerRelative.addView(subject, subjectLP);
			//creare TextView click to read
			TextView click = new TextView(context);
			click.setTextSize(22.0f);
			click.setText("Click to read message");
			click.setTextColor(Color.WHITE);
			click.setGravity(Gravity.CENTER_HORIZONTAL);
			RelativeLayout.LayoutParams clickLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
	        		RelativeLayout.LayoutParams.WRAP_CONTENT);
			clickLP.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, innerRelative.getId());
			innerRelative.addView(click, clickLP);
			relativeL.addView(innerRelative);
		}
	}

	public boolean onTouchEvent(MotionEvent event) {
		if (!touched) {
			if ((new Region((int)left_value, (int)top_value, 
					(int)right_value, (int)bottom_value)).
					contains((int)event.getX(), (int)event.getY()))
			{
				int action = event.getAction();
				if (action == MotionEvent.ACTION_DOWN) {
					firstTouchTime = event.getEventTime();
					return true;
				} else if (action == MotionEvent.ACTION_UP && (firstTouchTime - event.getEventTime()) < 300) { 
			    	activity.scaleUp(this);
			    	return true;
			    }
			}
		} else {
			left_value = x - x_length * scale;
			right_value = x + x_length * scale;
			top_value = y - y_length* scale;
			bottom_value = y + y_length * scale;
			if ((new Region((int)left_value, (int)top_value, 
					(int)right_value, (int)bottom_value)).
					contains((int)event.getX(), (int)event.getY()))
			{
				int action = event.getAction();
				if (action == MotionEvent.ACTION_DOWN) {
					firstTouchTime = event.getEventTime();
					return true;
				} else if (action == MotionEvent.ACTION_UP && (firstTouchTime - event.getEventTime()) < 200) {
					activity.goToReadMessageActivity(this);
			    	return true;
				}
			} else {
				int action = event.getAction();
				if (action == MotionEvent.ACTION_DOWN) {
					firstTouchTime = event.getEventTime();
					return true;
				} else if (action == MotionEvent.ACTION_UP && (firstTouchTime - event.getEventTime()) < 200) {
					activity.scaleDown();
			    	return true;
				}
			}
		}
		return false;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public void setLocation(float f, float g, float h) {
		x = f;
		y = g;
		scale = h;
		invalidate();
	}

	public void touch() {
		this.touched = !touched;
	}

}
